import 'package:fishing/models/pond_seat.dart';
import 'package:fishing/models/appointment.dart';
import './http_client.dart';
import '../models/notice.dart'; // 你的 Notice 模型类
import '../models/fishing_spot.dart'; // 钓鱼点模型类
import '../models/pond.dart'; // 鱼塘模型类

class HomeService {
  final HttpClient _httpClient = HttpClient();

  Future<List<Notice>> getNoticeList(
      {int pageNum = 1, int pageSize = 10}) async {
    try {
      final response = await _httpClient.get(
        '/pond/notice/list',
        queryParameters: {
          'pageNum': pageNum,
          'pageSize': pageSize,
        },
      );


      if (response['code'] == 200) {
        final List<dynamic> rows = response['rows'] ?? [];
        return rows.map((e) => Notice.fromJson(e)).toList();
      } else {
        return [];
      }
    } catch (e, stack) {
      return [];
    }
  }

  Future<List<FishingSpot>> getFishingSpotList({
    int pageNum = 1,
    int pageSize = 10,
    double? userLatitude,
    double? userLongitude,
  }) async {
    try {
      Map<String, dynamic> queryParameters = {
        'pageNum': pageNum,
        'pageSize': pageSize,
      };

      // 添加用户位置参数，默认使用广州番禺区坐标
      queryParameters['userLatitude'] = userLatitude ?? 22.9375;
      queryParameters['userLongitude'] = userLongitude ?? 113.3642;

      final response = await _httpClient.get(
        '/pond/app/fishingSpot/list',
        queryParameters: queryParameters,
      );


      if (response['code'] == 200) {
        final List<dynamic> data = response['data'] ?? [];
        return data.map((e) => FishingSpot.fromJson(e)).toList();
      } else {
        return [];
      }
    } catch (e, stack) {
      return [];
    }
  }

  // 获取钓场详情
  Future<FishingSpot?> getFishingSpotDetail(String id) async {
    try {
      final response = await _httpClient.get('/pond/app/fishingSpot/$id');


      if (response['code'] == 200) {
        final data = response['data'];
        if (data != null) {
          return FishingSpot.fromJson(data);
        }
      } else {
      }
      return null;
    } catch (e, stack) {
      return null;
    }
  }

  // 获取钓场详情和鱼塘列表
  Future<Map<String, dynamic>?> getFishingSpotDetailWithPonds(
    String id, {
    double? userLatitude,
    double? userLongitude,
  }) async {
    try {
      Map<String, dynamic> queryParameters = {};

      // 添加用户位置参数
      if (userLatitude != null && userLongitude != null) {
        queryParameters['userLatitude'] = userLatitude;
        queryParameters['userLongitude'] = userLongitude;
      }else{
        queryParameters['userLatitude'] = 22.9375;
        queryParameters['userLongitude'] = 113.3642;
      }

      final response = await _httpClient.get(
        '/pond/app/fishingSpot/$id',
        queryParameters: queryParameters.isNotEmpty ? queryParameters : null,
      );

      if (response['code'] == 200) {
        final data = response['data'];
        if (data != null) {

          // 解析钓场信息
          final fishingSpot = FishingSpot.fromJson(data);

          // 解析鱼塘列表
          List<Pond> ponds = [];
          if (data['pondList'] != null) {
            final pondList = data['pondList'] as List;
            ponds = pondList.map((e) => Pond.fromJson(e)).toList();
          } else {
          }

          return {
            'fishingSpot': fishingSpot,
            'ponds': ponds,
          };
        } else {
        }
      } else {
      }
      return null;
    } catch (e, stack) {
      return null;
    }
  }

  // 获取鱼塘列表
  Future<List<Pond>> getPondList({
    int pageNum = 1,
    int pageSize = 10,
    required int deptId,
  }) async {
    try {
      final response = await _httpClient.get(
        '/pond/pond/list',
        queryParameters: {
          'pageNum': pageNum,
          'pageSize': pageSize,
          'deptId': deptId,
        },
      );


      if (response['code'] == 200) {
        final List<dynamic> rows = response['rows'] ?? [];
        return rows.map((e) => Pond.fromJson(e)).toList();
      } else {
        return [];
      }
    } catch (e, stack) {
      return [];
    }
  }

  Future<List<PondSeat>> getPondSeatList({required int deptId, required int priceSegmentId, double userLatitude = 22.9375, double userLongitude = 113.3642}) async {
    try {
      final response = await _httpClient.get('/pond/seat/list',
          queryParameters: {
            'deptId': deptId,
            'priceSegmentId': priceSegmentId
          });

      if (response['code'] == 200) {
        final List<dynamic> rows = response['rows'] ?? [];
        final seats = rows.map((e) => PondSeat.fromJson(e)).toList();
        return seats;
      } else {
        return [];
      }
    } catch (e) {
      return [];
    }
  }

  // 创建预约
  Future<bool> createAppointment({
    required int deptId,
    required int pondId,
    required int userId,
    required List<Map<String, dynamic>> timeSegments,
    int status = 0, // 默认状态为0
  }) async {
    try {
      final response = await _httpClient.post(
        '/pond/appointment',
        data: {
          'userId': userId,
          'deptId': deptId,
          'pondId': pondId,
          'status': status,
          'timeSegments': timeSegments,
        },
      );


      if (response['code'] == 200) {
        return true;
      } else {
        return false;
      }
    } catch (e, stack) {
      return false;
    }
  }

  // 获取用户预约列表
  Future<List<Appointment>> getUserAppointments() async {
    try {
      final response = await _httpClient.get('/pond/appointment/user');


      if (response['code'] == 200) {
        final List<dynamic> data = response['data'] ?? [];
        return data.map((e) => Appointment.fromJson(e)).toList();
      } else {
        return [];
      }
    } catch (e, stack) {
      return [];
    }
  }

  // 取消预约
  Future<bool> cancelAppointment(int appointmentId) async {
    try {
      final response = await _httpClient.put('/pond/appointment/$appointmentId/cancel');


      if (response['code'] == 200) {
        return true;
      } else {
        return false;
      }
    } catch (e, stack) {
      return false;
    }
  }

  // 签到
  Future<bool> checkinTimeSegment(int timeSegmentId) async {
    try {
      final response = await _httpClient.get('/pond/app/checkin/$timeSegmentId');


      if (response['code'] == 200) {
        return true;
      } else {
        return false;
      }
    } catch (e, stack) {
      return false;
    }
  }
}
